#from time import sleep
import os

'''
读入所在文件夹的以有序数字为(部分)文件名的m3u8的分片文件，合并，输出一个完整的mp4文件
'''

input_dir = input('Dir : ')
os.chdir(input_dir)

l = os.listdir(input_dir)
prestr = l[0].strip('1234567890')
prestr_len=len(prestr)

l_ = []
for i in l:
    try:
        l_.append(int(i[prestr_len:]))
    except:
        pass
l = l_
l.sort(key=int)

with open(prestr+'.mp4', "ab+") as o_f:
    for i in l:
        print('Merge : ',str(l[i]),' / ',str(l[-1]),end='\r')
        with open(prestr+str(i), "rb") as i_f:
            o_f.write(i_f.read())

print('Merge : Done : ',prestr,'.mp4')
